package wechat.base.snd;

import com.google.common.base.Objects;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.json.JSONObject;
import wechat.base.snd.Contact;
import wechat.base.snd.ErrorMsg;
import wechat.base.snd.WCApplication;
import wechat.base.snd.util.ExecPost;

@SuppressWarnings("all")
public class ContactManager {
  private static String WECHAT_CONTACTS_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token=";
  
  private static ContactManager customerManager = null;
  
  private WCApplication _corpApp;
  
  public static ContactManager getContactManager(final WCApplication corpApp) {
    return new ContactManager(corpApp);
  }
  
  public ContactManager(final WCApplication corpApp) {
    boolean _equals = Objects.equal(ContactManager.customerManager, null);
    if (_equals) {
      ContactManager _contactManager = new ContactManager(corpApp);
      ContactManager.customerManager = _contactManager;
      this._corpApp = corpApp;
    }
  }
  
  public String getContactUrl() {
    String _token = this._corpApp.getToken();
    return (ContactManager.WECHAT_CONTACTS_URL + _token);
  }
  
  public ErrorMsg addContact(final Contact c) {
    try {
      String _excuteJsonPost = ExecPost.excuteJsonPost(this.getContactUrl(), c.jsonBuilder().toString());
      JSONObject _jSONObject = new JSONObject(_excuteJsonPost);
      return new ErrorMsg(_jSONObject);
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
}
